var sysId;
var editIndex = undefined;
var isHasData = false;
var templetId = "";
var editCodeIndex = undefined;
var type; // 当前字段类型选择选择的值
var selectTypeCode = undefined; // 当前【类型选择】选择的值 默认为 undefined
var selectTypeName = undefined; // 当前【类型选择】选择的值 默认为 undefined
var selectTypeFlag = true;

var treeType = 'info';//判断是主组织树还是子组织树 info：自组织 ；root：根组织

/**
 * 用于单元格编辑是重写的方法
 */
$.extend($.fn.datagrid.methods, {
    editCell: function(jq, param) {
        return jq.each(function() {
            var fields = $(this).datagrid('getColumnFields', true).concat(
                $(this).datagrid('getColumnFields'));
            for (var i = 0; i < fields.length; i++) {
                var col = $(this).datagrid('getColumnOption', fields[i]);
                col.editor1 = col.editor;
                if (fields[i] != param.field) {
                    var fieldOpts = $(this).datagrid('getColumnOption',
                        fields[i]);
                    if (!fieldOpts.hidden || fieldOpts.hidden == false) {
                        col.editor = null;
                    }
                }
            }
            $(this).datagrid('beginEdit', param.index);
            for (var i = 0; i < fields.length; i++) {
                var col = $(this).datagrid('getColumnOption', fields[i]);
                col.editor = col.editor1;
            }
        });
    },
    addEditor: function(jq, param) {
        if (param instanceof Array) {
            $.each(param, function(index, item) {
                var e = $(jq).datagrid('getColumnOption', item.field);
                e.editor = item.editor;
            });
        } else {
            var e = $(jq).datagrid('getColumnOption', param.field);
            e.editor = param.editor;
        }
    },
    removeEditor: function(jq, param) {
        if (param instanceof Array) {
            $.each(param, function(index, item) {
                var e = $(jq).datagrid('getColumnOption', item);
                e.editor = {};
            });
        } else {
            var e = $(jq).datagrid('getColumnOption', param);
            e.editor = {};
        }
    }
});
/**
 * 增加记录
 */
function addForm(add) {
	var node;
	if(treeType == "root"){
		node = $('#commTempletRootTree').tree('getSelected');
	}else{
		node = $('#commTempletInfoTree').tree('getSelected');
	}
    var type = node.attributes.TEMP_TYPE;
    var dialogId = "FormAddDialog";
    var toActionUrl = "platform/aos/commtempletmanagerController/toCommTempletAdd?dialogId=" + dialogId + "&pid=" + node.id + "&type=" + type + "&sysId=" + sysId;
    //commonLang.dialog.add"添加"
    var usdAdd = new CommonDialog(dialogId, "650", "300", toActionUrl, commonLang.dialog.add,
        false, true, false);
    usdAdd.show();
}

/**
 * 初始化字段维护
 */
function config() {
	var node;
	if(treeType == "root"){
		node = $('#commTempletRootTree').tree('getSelected');
	}else{
		node = $('#commTempletInfoTree').tree('getSelected');
	}
    var tId = node.id;
    var dialogId = "formTempletDialog";
    var toActionUrl = "platform/aos/commtempletmanagerController/toInitFieldConfig?dialogId=" + dialogId + "&tId=" + tId + "&sysId=" + sysId;
    //commonLang.dialog.initFieldMaintain "初始化字段维护"
    var usdAddConfig = new CommonDialog(dialogId, "", "", toActionUrl,
    		commonLang.dialog.initFieldMaintain, false, true, false, "", true);
    usdAddConfig.show();
}

/**
 * 修改记录
 */
function editForm() {
	var node;
	if(treeType == "root"){
		node = $('#commTempletRootTree').tree('getSelected');
	}else{
		node = $('#commTempletInfoTree').tree('getSelected');
	}
    var id = node.id;
    var dialogId = "FormEditDialog";
    toJspUrl = "avicit/cbb/techclass/tabledefine/commtempletmanager/comm_templet_edit";
    toActionUrl = "platform/aos/commtempletmanagerController/toCommTempletEdit?id=" + id + "&dialogId=" + dialogId + "&toJspUrl=" + toJspUrl;
    //commonLang.dialog.edit "编辑"
    var usdEdit = new CommonDialog(dialogId, "650", "300", toActionUrl, commonLang.dialog.edit,
        false, true, false);
    usdEdit.show();
}

/**
 * 删除记录
 */
function deleteForm() {
	var node;
	if(treeType == "root"){
		node = $('#commTempletRootTree').tree('getSelected');
	}else{
		node = $('#commTempletInfoTree').tree('getSelected');
	}
    $.messager
        .confirm(
            //commonLang.tip.confirm'请确认'，commonLang.tip.confirmContent '您确定要删除当前所选的数据？'
        		commonLang.tip.confirm,
        		commonLang.tip.confirmContent,
            function(b) {
                if (b) {
                    $
                        .ajax({
                            url: 'platform/aos/commtempletmanagerController/deleteCommTempletInfo',
                            data: {
                                id: node.id
                            },
                            type: 'post',
                            dataType: 'json',
                            success: function(result) {
                                if (result.flag == "success") {
                                    reloadCommTempletInfoTree();
                                    reloadCommTempletRootTree();
                                    $.messager.show({
                                        title: commonLang.tip.alert,//'提示',
                                        //commonLang.optip.success "删除成功！"
                                        msg: commonLang.optip.success,
                                        timeout: 2000,
                                        showType: 'slide'
                                    });
                                } else {
                                    if (result.flag == "failure") {
                                        $.messager.show({
                                            title: commonLang.tip.alert,// '提示',
                                            //commonLang.optip.failure参数替换"删除失败！"
                                            msg: commonLang.optip.failure,
                                            timeout: 2000,
                                            showType: 'slide'
                                        });
                                    } else {
                                        $.messager
                                            .alert(
                                            		commonLang.tip.alert,//'提示',
                                                result.flag,
                                                'warning');
                                    }
                                }
                            }
                        });
                }
            });
}

/**
 * 删除记录
 */
function deleteDetailForm() {
    var rows = $('#commTempletFieldDataGrid').datagrid('getChecked');
    var ids = [];
    if (rows.length > 0) {
        for (var i = 0; i < rows.length; i++) {
            var colIsSys = rows[i].colIsSys;
            if ('Y' == colIsSys) {
                //commonLang.tip.alert '提示',commonLang.tip.content'请勿删除系统字段！'
            	$.messager.alert(commonLang.tip.alert,commonLang.tip.content,'warning');
                return;
            }
        }
        $.messager
            .confirm(
                //commonLang.tip.confirm参数替换'请确认',commonLang.tip.confirmContent参数替换'您确定要删除当前所选的数据？'
            		commonLang.tip.confirm,
            		commonLang.tip.confirmContent,
                function(b) {
                    if (b) {
                        for (var i = 0, length = rows.length; i < length; i++) {
                            ids.push(rows[i].id);
                        }
                        $
                            .ajax({
                                url: 'platform/aos/commtempletmanagerController/deleteCommTempletField',
                                data: {
                                    ids: ids.join(','),
                                    sysId: sysId
                                },
                                type: 'post',
                                dataType: 'json',
                                success: function(result) {
                                    if (result.flag == "success") {
                                        reloadTempletInfo();
                                        $.messager.show({
                                            title: commonLang.tip.alert,//'提示',
                                            //commonLang.optip.success参数替换"删除成功！"
                                            msg: commonLang.optip.success,
                                            timeout: 2000,
                                            showType: 'slide'
                                        });
                                    } else {
                                        if (result.flag == "failure") {
                                            $.messager.show({
                                                title: commonLang.tip.alert,//'提示',
                                                //commonLang.optip.failure参数替换"删除失败！"
                                                msg: commonLang.optip.failure,
                                                timeout: 2000,
                                                showType: 'slide'
                                            });
                                        } else {
                                            $.messager.alert(commonLang.tip.alert,//'提示',
                                                result.flag,
                                                'warning');
                                        }
                                    }
                                }
                            });
                    }
                });
    } else {
        //commonLang.tip.alert,'提示',commonLang.tip.content2参数替换'请选择要删除的记录！'
    	$.messager.alert(commonLang.tip.alert, commonLang.tip.content2, 'warning');
    }
}

/**
 * 刷新表格
 */
function reloadTempletInfo() {
	var node;
	if(treeType == "root"){
		node = $('#commTempletRootTree').tree('getSelected');
	}else{
		node = $('#commTempletInfoTree').tree('getSelected');
	}
    reloadDataGridByInfoId(node.id);
}
/**
 * 根据主表ID刷新子表表格数据
 */
function reloadDataGridByInfoId(infoId) {
	var url = "platform/aos/commtempletmanagerController/getCommTempletFieldData.json?treeType="+treeType+"&tid=" + infoId + "&isStructure=N";
    $('#commTempletFieldDataGrid').datagrid("options").url = url;
    $("#commTempletFieldDataGrid").datagrid('load', {
        param: null
    });
}

//SanXiaFiles Create By Tangkun start
function getResultByInput(){
	var tid = templetId;
	$('#commTempletFieldDataGrid').datagrid({
		url :  "platform/aos/commtempletmanagerController/getCommTempletFieldData.json?tid=" + tid +"&treeType="+ treeType + "&isStructure=N&inputWord=" + encodeURI(encodeURI($('#query').val()))
	});
}
function keyDownQuery(){
	if(event.keyCode==13){
		getResultByInput();
	}
}
// SanXiaFiles Create By Tangkun End

/**
 * 新增行时，选择类型，填充默认长度
 * 
 * @param record
 */
function typeSelect(record) {
    var rowDataNew = $('#commTempletFieldDataGrid').datagrid('getData').rows[editIndex]; // 当前行
    var isNewNew = "";
    if (rowDataNew != undefined) {
        isNewNew = rowDataNew.isNew;
    }
    if (isNewNew == "Y") {
        var type = record.lookupCode;
        if (type == "string") {
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                editIndex);
            var sfgzEditor = editors[3];
            sfgzEditor.target.val(200);
        } else if (type == "integer") {
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                editIndex);
            var sfgzEditor = editors[3];
            sfgzEditor.target.val(16);
        } else if (type == "float") {
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                editIndex);
            var sfgzEditor1 = editors[3];
            sfgzEditor1.target.val(16);
            var sfgzEditor2 = editors[4];
            sfgzEditor2.target.val(2);
        } else {
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                editIndex);
            var sfgzEditor1 = editors[3];
            sfgzEditor1.target.val("");
            var sfgzEditor2 = editors[4];
            sfgzEditor2.target.val("");
        }
    }
}




/**
 * 左侧分类树加载方法
 */
function reloadCommTempletInfoTree() {
    $('#commTempletInfoTree')
        .tree({
            checkbox: false,
            /* 是否带复选框 */
            lines: false,
            /* 线条 */
            method: 'post',
            /* 请求数据方式 */
            url: 'platform/aos/commtempletmanagerController/findChildsNode?parentId=ROOT&sysId='+sysId,
            /* 请求数据地址 */
            dataType: "json",
            /* 数据格式 */
            onClick: function(node) {
            	treeType = "info";
                currentDeptId = node.id;
                templetId = node.id;
                currentDeptName = node.text;
                if (node.attributes.TEMP_TYPE == "C") {
                    reloadDataGridByInfoId(currentDeptId);
                    $('#addTempField').show();
                    $('#saveTempField').show();
                    $('#deleteTempField').show();
                    $('#copyField').show();
                    $('#export').show();
                    $('#importExcel').show();
                    $('#exportExcelMoudle').show();

                    $('#searchField').show();
                    $('#appendCode').show();
                    $('#saveCode').show();
                    $('#deleteWcode').show();
                    $('#selectCode').show();

                    $('#configField').hide();
                } else if (node.attributes.TEMP_TYPE == "F") {
                    reloadDataGridByInfoId("@@@@");
                    $('#addTempField').hide();
                    $('#saveTempField').hide();
                    $('#deleteTempField').hide();
                    $('#copyField').hide();
                    $('#export').hide();
                    $('#importExcel').hide();
                    $('#exportExcelMoudle').hide();
                    $('#configField').hide();

                    $('#searchField').hide();
                    $('#appendCode').hide();
                    $('#saveCode').hide();
                    $('#deleteWcode').hide();
                    $('#selectCode').hide();
                } else if (node.attributes.TEMP_TYPE == "R") {
                    reloadDataGridByInfoId("@@@@");
                    $('#addTempField').hide();
                    $('#saveTempField').hide();
                    $('#deleteTempField').hide();
                    $('#copyField').hide();
                    $('#export').hide();
                    $('#importExcel').hide();
                    $('#exportExcelMoudle').hide();
                    $('#configField').hide();

                    $('#searchField').hide();
                    $('#appendCode').hide();
                    $('#saveCode').hide();
                    $('#deleteWcode').hide();
                    $('#selectCode').hide();
                } else if (node.attributes.TEMP_TYPE == "O") {
                    reloadDataGridByInfoId("@@@@");
                    $('#addTempField').hide();
                    $('#saveTempField').hide();
                    $('#deleteTempField').hide();
                    $('#copyField').hide();
                    $('#export').hide();
                    $('#importExcel').hide();
                    $('#exportExcelMoudle').hide();
                    $('#configField').hide();

                    $('#searchField').hide();
                    $('#appendCode').hide();
                    $('#saveCode').hide();
                    $('#deleteWcode').hide();
                    $('#selectCode').hide();
                } else if (node.attributes.TEMP_TYPE == "S") {
                    reloadDataGridByInfoId("@@@@");
                    $('#addTempField').hide();
                    $('#saveTempField').hide();
                    $('#deleteTempField').hide();
                    $('#copyField').hide();
                    $('#export').hide();
                    $('#importExcel').hide();
                    $('#exportExcelMoudle').hide();
                    $('#configField').show();

                    $('#searchField').hide();
                    $('#appendCode').hide();
                    $('#saveCode').hide();
                    $('#deleteWcode').hide();
                    $('#selectCode').hide();
                }
            },
            loadFilter: function(data) {
                // 过滤后台返回的数据，处理不同json结构
                if (data.data) {
                    return data.data;
                } else {
                    return data;
                }
            },
            onLoadSuccess: function(node, data) {
                var newNode = $('#commTempletInfoTree').tree(
                    "getRoot");
                if (newNode != null) {
                    $('#commTempletInfoTree').tree('select',
                        newNode.target);
                    currentDeptId = newNode.id;
                    currentDeptName = newNode.text;
                    reloadDataGridByInfoId("@@@@@@@@@");
                }
            },
            onContextMenu: function(e, node) {
            	treeType = "info";
                e.preventDefault();
                // 查找节点
                $('#commTempletInfoTree').tree('select',
                    node.target);
                // 显示快捷菜单
                $('#mm').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
                if (node.attributes.TEMP_TYPE == "C") {
                    document.getElementById("muAdd").style.display = 'none';
                    document.getElementById("muRemove").style.display = '';
                    document.getElementById("muEdit").style.display = '';
                } else if (node.attributes.TEMP_TYPE == "R") {
                    document.getElementById("muAdd").style.display = 'none';
                    document.getElementById("muRemove").style.display = 'none';
                    document.getElementById("muEdit").style.display = 'none';
                } else if (node.attributes.TEMP_TYPE == "O") {
                    document.getElementById("muAdd").style.display = '';
                    document.getElementById("muRemove").style.display = 'none';
                    document.getElementById("muEdit").style.display = 'none';
                } else if (node.attributes.TEMP_TYPE == "S") {
                    document.getElementById("muAdd").style.display = '';
                    document.getElementById("muRemove").style.display = 'none';
                    document.getElementById("muEdit").style.display = '';
                } else if (node.attributes.TEMP_TYPE == "F") {
                    var isLeaf = $('#commTempletInfoTree').tree(
                        'isLeaf', node.target);
                    if (!isLeaf) {
                        document.getElementById("muRemove").style.display = 'none';
                    } else {
                        document.getElementById("muRemove").style.display = '';
                    }
                    document.getElementById("muAdd").style.display = '';
                    document.getElementById("muEdit").style.display = '';
                }
            },
            formatter : function(node) {
				return '<span title="' + node.text + '">' + node.text + '</span>';
			}
        });
}



/**
 * 左侧根组织分类树加载方法
 */
function reloadCommTempletRootTree() {
    $('#commTempletRootTree')
        .tree({
            checkbox: false,
            /* 是否带复选框 */
            lines: false,
            /* 线条 */
            method: 'post',
            /* 请求数据方式 */
            url: 'platform/aos/commtempletmanagerController/findChildsNodeRoot?parentId=ROOT&sysId='+sysId,
            /* 请求数据地址 */
            dataType: "json",
            /* 数据格式 */
            onClick: function(node) {
            	treeType = "root";
                currentDeptId = node.id;
                templetId = node.id;
                currentDeptName = node.text;
                if (node.attributes.TEMP_TYPE == "C") {
                    reloadDataGridByInfoId(currentDeptId);
                    if(orgId == "ORG_ROOT"){
                    	 $('#addTempField').show();
                         $('#saveTempField').show();
                         $('#deleteTempField').show();
                         $('#copyField').show();
                         $('#export').show();
                         $('#importExcel').show();
                         $('#exportExcelMoudle').show();
                         $('#searchField').show();
                         $('#appendCode').show();
                         $('#saveCode').show();
                         $('#deleteWcode').show();
                         $('#selectCode').show();
                         
                         $('#configField').hide();
                    }else{
                    	$('#addTempField').hide();
                        $('#saveTempField').hide();
                        $('#deleteTempField').hide();
                        $('#copyField').hide();
                        $('#export').hide();
                        $('#importExcel').hide();
                        $('#exportExcelMoudle').hide();
                        $('#searchField').hide();
                        $('#appendCode').hide();
                        $('#saveCode').hide();
                        $('#deleteWcode').hide();
                        $('#selectCode').hide();
                        $('#configField').hide();
                        
                    }
                } else if (node.attributes.TEMP_TYPE == "F") {
                    reloadDataGridByInfoId("@@@@");
                    $('#addTempField').hide();
                    $('#saveTempField').hide();
                    $('#deleteTempField').hide();
                    $('#copyField').hide();
                    $('#export').hide();
                    $('#importExcel').hide();
                    $('#exportExcelMoudle').hide();
                    $('#searchField').hide();
                    $('#appendCode').hide();
                    $('#saveCode').hide();
                    $('#deleteWcode').hide();
                    $('#selectCode').hide();
                    $('#configField').hide();
                } else if (node.attributes.TEMP_TYPE == "R") {
                    reloadDataGridByInfoId("@@@@");
                    $('#addTempField').hide();
                    $('#saveTempField').hide();
                    $('#deleteTempField').hide();
                    $('#copyField').hide();
                    $('#export').hide();
                    $('#importExcel').hide();
                    $('#exportExcelMoudle').hide();
                    $('#searchField').hide();
                    $('#appendCode').hide();
                    $('#saveCode').hide();
                    $('#deleteWcode').hide();
                    $('#selectCode').hide();
                } else if (node.attributes.TEMP_TYPE == "O") {
                    reloadDataGridByInfoId("@@@@");
                    $('#addTempField').hide();
                    $('#saveTempField').hide();
                    $('#deleteTempField').hide();
                    $('#copyField').hide();
                    $('#export').hide();
                    $('#importExcel').hide();
                    $('#exportExcelMoudle').hide();
                    $('#searchField').hide();
                    $('#appendCode').hide();
                    $('#saveCode').hide();
                    $('#deleteWcode').hide();
                    $('#selectCode').hide();
                    $('#configField').hide();
                } else if (node.attributes.TEMP_TYPE == "S") {
                    reloadDataGridByInfoId("@@@@");
                    $('#addTempField').hide();
                    $('#saveTempField').hide();
                    $('#deleteTempField').hide();
                    $('#copyField').hide();
                    $('#export').hide();
                    $('#importExcel').hide();
                    $('#exportExcelMoudle').hide();
                    $('#searchField').hide();
                    $('#appendCode').hide();
                    $('#saveCode').hide();
                    $('#deleteWcode').hide();
                    $('#selectCode').hide();
                    if(orgId == "ORG_ROOT"){
                    	$('#configField').show();
                    }else{
                    	$('#configField').hide();
                    }
                }
            },
            loadFilter: function(data) {
                // 过滤后台返回的数据，处理不同json结构
                if (data.data) {
                    return data.data;
                } else {
                    return data;
                }
            },
            onLoadSuccess: function(node, data) {
                var newNode = $('#commTempletRootTree').tree(
                    "getRoot");
                if (newNode != null) {
                    $('#commTempletRootTree').tree('select',
                        newNode.target);
                    currentDeptId = newNode.id;
                    currentDeptName = newNode.text;
                    reloadDataGridByInfoId("@@@@@@@@@");
                }
            },
            onContextMenu: function(e, node) {
            	if(orgId == "ORG_ROOT"){
            		treeType = "root";
                    e.preventDefault();
                    // 查找节点
                    $('#commTempletRootTree').tree('select',
                        node.target);
                    // 显示快捷菜单
                    $('#mm').menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    });
                    if (node.attributes.TEMP_TYPE == "C") {
                        document.getElementById("muAdd").style.display = 'none';
                        document.getElementById("muRemove").style.display = '';
                        document.getElementById("muEdit").style.display = '';
                    } else if (node.attributes.TEMP_TYPE == "R") {
                        document.getElementById("muAdd").style.display = 'none';
                        document.getElementById("muRemove").style.display = 'none';
                        document.getElementById("muEdit").style.display = 'none';
                    } else if (node.attributes.TEMP_TYPE == "O") {
                        document.getElementById("muAdd").style.display = '';
                        document.getElementById("muRemove").style.display = 'none';
                        document.getElementById("muEdit").style.display = 'none';
                    } else if (node.attributes.TEMP_TYPE == "S") {
                        document.getElementById("muAdd").style.display = '';
                        document.getElementById("muRemove").style.display = 'none';
                        document.getElementById("muEdit").style.display = '';
                    } else if (node.attributes.TEMP_TYPE == "F") {
                        var isLeaf = $('#commTempletRootTree').tree(
                            'isLeaf', node.target);
                        if (!isLeaf) {
                            document.getElementById("muRemove").style.display = 'none';
                        } else {
                            document.getElementById("muRemove").style.display = '';
                        }
                        document.getElementById("muAdd").style.display = '';
                        document.getElementById("muEdit").style.display = '';
                    }
            	}
            },
            formatter : function(node) {
				return '<span title="' + node.text + '">' + node.text + '</span>';
			}
        });
}




/**
 * 【字段类型】转换方法
 * 
 * @param value
 * @param row
 * @param index
 * @returns
 */
function fomaterColType(value, row, index) {
	if (value == 'string') {
        return commonLang.dataType.string;//"字符串";
    } else if (value == 'integer') {
        return  commonLang.dataType.integer;//"整型";
    } else if (value == 'date') {
        return  commonLang.dataType.date;//"日期时间型";
    } else if (value == 'float') {
        return  commonLang.dataType.float;//"浮点型";
    } else if (value == 'blob') {
        return "BLOB";
    } else if (value == 'clob') {
        return "CLOB";
    }
    return value;
}

/**
 * 【系统字段】转换方法
 * 
 * @param value
 * @param row
 * @param index
 * @returns
 */
//function fomaterIsSys(value, row, index) {
//	var c = "";
//	if (value == "Y") {
//		c = "<input type='checkbox' checked='checked' value='Y' disabled='disabled'/>";
//	} else {
//		c = "<input type='checkbox' value='N' disabled='disabled'/>";
//	}
//	return c;
//}
function fomaterIsSys(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsSys == "Y") {
        c = "<input type='checkbox' id='" + rowData.id + "IsSys' checked='checked' value='Y' onclick='onCheckIsSys(\"" + rowData.id + "IsSys\"," + rowIndex + ")' />";
    } else {
        c = "<input type='checkbox' id='" + rowData.id + "IsSys' value='N' onclick='onCheckIsSys(\"" + rowData.id + "IsSys\"," + rowIndex + ")' />";
    }
    return c;
}

/**
 * 【是否系统字段】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsSys(id, rowIndex) {
    if (endEditing()) {
        $("#commTempletFieldDataGrid").datagrid('removeEditor', 'colIsSys'); // 删除【是否必著】编辑器
        $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否必著】编辑器
            field: 'colIsSys',
            editor: {
                type: 'text'
            }
        });
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[5];
            sfgzEditor.target.val("Y");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        } else {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[5];
            sfgzEditor.target.val("N");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        }
    } else {
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            document.getElementById(id).checked = false;
        } else {
            document.getElementById(id).checked = true;
        }
    }
}

/**
 * 【是否显示】转换方法
 * 
 * @param value
 * @param rowData
 * @param rowIndex
 * @returns {String}
 */
function fomaterIsShow(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsDisplay == "Y") {
        c = "<input type='checkbox' id='" + rowData.id + "IsShow' checked='checked' value='Y' onclick='onCheckIsShow(\"" + rowData.id + "IsShow\"," + rowIndex + ")' />";
    } else {
        c = "<input type='checkbox' id='" + rowData.id + "IsShow' value='N' onclick='onCheckIsShow(\"" + rowData.id + "IsShow\"," + rowIndex + ")' />";
    }
    return c;
}

/**
 * 【是否显示】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsShow(id, rowIndex) {
    if (endEditing()) {
        $("#commTempletFieldDataGrid").datagrid('removeEditor', 'colIsDisplay'); // 删除【是否显示】编辑器
        $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否显示】编辑器
            field: 'colIsDisplay',
            editor: {
                type: 'text'
            }
        });
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[6];
            sfgzEditor.target.val("Y");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        } else {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[6];
            sfgzEditor.target.val("N");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        }
    } else {
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            document.getElementById(id).checked = false;
        } else {
            document.getElementById(id).checked = true;
        }
    }
}

/**
 * 【是否必著】转换方法
 * 
 * @param value
 * @param rowData
 * @param rowIndex
 * @returns {String}
 */
function fomaterIsMust(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsMust == "Y") {
        c = "<input type='checkbox' id='" + rowData.id + "IsMust' checked='checked' value='Y' onclick='onCheckIsMust(\"" + rowData.id + "IsMust\"," + rowIndex + ")'/>";
    } else {
        c = "<input type='checkbox' id='" + rowData.id + "IsMust' value='N' onclick='onCheckIsMust(\"" + rowData.id + "IsMust\"," + rowIndex + ")'/>";
    }
    return c;
}

/**
 * 【是否必著】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsMust(id, rowIndex) {
    if (endEditing()) {
        $("#commTempletFieldDataGrid").datagrid('removeEditor', 'colIsMust'); // 删除【是否必著】编辑器
        $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否必著】编辑器
            field: 'colIsMust',
            editor: {
                type: 'text'
            }
        });
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[7];
            sfgzEditor.target.val("Y");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        } else {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[7];
            sfgzEditor.target.val("N");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        }
    } else {
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            document.getElementById(id).checked = false;
        } else {
            document.getElementById(id).checked = true;
        }
    }
}

/**
 * 【是否显示（添加/编辑）】转换方法
 * 
 * @param value
 * @param rowData
 * @param rowIndex
 * @returns {String}
 */
function fomaterIsVisible(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsVisible == "Y") {
        c = "<input type='checkbox' id='" + rowData.id + "IsVisible' checked='checked' value='Y' onclick='onCheckIsVisible(\"" + rowData.id + "IsVisible\"," + rowIndex + ")' />";
    } else {
        c = "<input type='checkbox' id='" + rowData.id + "IsVisible' value='N' onclick='onCheckIsVisible(\"" + rowData.id + "IsVisible\"," + rowIndex + ")' />";
    }
    return c;
}

/**
 * 【是否显示（添加/编辑）】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsVisible(id, rowIndex) {
    if (endEditing()) {
        $("#commTempletFieldDataGrid").datagrid('removeEditor', 'colIsVisible'); // 删除【是否显示（添加/编辑）】编辑器
        $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否显示（添加/编辑）】编辑器
            field: 'colIsVisible',
            editor: {
                type: 'text'
            }
        });
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[8];
            sfgzEditor.target.val("Y");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        } else {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[8];
            sfgzEditor.target.val("N");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        }
    } else {
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            document.getElementById(id).checked = false;
        } else {
            document.getElementById(id).checked = true;
        }
    }
}

/**
 * 【是否编辑（添加/编辑）】转换方法
 * 
 * @param value
 * @param rowData
 * @param rowIndex
 * @returns {String}
 */
function fomaterIsEdit(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsEdit == "Y") {
        c = "<input type='checkbox' id='" + rowData.id + "IsEdit' checked='checked' value='Y' onclick='onCheckIsEdit(\"" + rowData.id + "IsEdit\"," + rowIndex + ")' />";
    } else {
        c = "<input type='checkbox' id='" + rowData.id + "IsEdit' value='N' onclick='onCheckIsEdit(\"" + rowData.id + "IsEdit\"," + rowIndex + ")' />";
    }
    return c;
}

/**
 * 【是否编辑（添加/编辑）】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsEdit(id, rowIndex) {
    if (endEditing()) {
        $("#commTempletFieldDataGrid").datagrid('removeEditor', 'colIsEdit'); // 删除【是否编辑（添加/编辑）】编辑器
        $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否编辑（添加/编辑）】编辑器
            field: 'colIsEdit',
            editor: {
                type: 'text'
            }
        });
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[9];
            sfgzEditor.target.val("Y");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        } else {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[9];
            sfgzEditor.target.val("N");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        }
    } else {
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            document.getElementById(id).checked = false;
        } else {
            document.getElementById(id).checked = true;
        }
    }
}

/**
 * 【表格显示】转换方法
 * 
 * @param value
 * @param rowData
 * @param rowIndex
 * @returns {String}
 */
function fomaterIsTabVisible(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsTabVisible == "Y") {
        c = "<input type='checkbox' id='" + rowData.id + "IsTabVisible' checked='checked' value='Y' onclick='onCheckIsTabVisible(\"" + rowData.id + "IsTabVisible\"," + rowIndex + ")' />";
    } else {
        c = "<input type='checkbox' id='" + rowData.id + "IsTabVisible' value='N' onclick='onCheckIsTabVisible(\"" + rowData.id + "IsTabVisible\"," + rowIndex + ")' />";
    }
    return c;
}

/**
 * 【表格显示】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsTabVisible(id, rowIndex) {
    if (endEditing()) {
        $("#commTempletFieldDataGrid").datagrid('removeEditor',
            'colIsTabVisible'); // 删除【表格显示】编辑器
        $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【表格显示】编辑器
            field: 'colIsTabVisible',
            editor: {
                type: 'text'
            }
        });
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[10];
            sfgzEditor.target.val("Y");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        } else {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[10];
            sfgzEditor.target.val("N");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        }
    } else {
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            document.getElementById(id).checked = false;
        } else {
            document.getElementById(id).checked = true;
        }
    }
}

/**
 * 【详细显示】转换方法
 * 
 * @param value
 * @param rowData
 * @param rowIndex
 * @returns {String}
 */
function fomaterIsDetail(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsDetail == "Y") {
        c = "<input type='checkbox' id='" + rowData.id + "IsDetail' checked='checked' value='Y' onclick='onCheckIsDetail(\"" + rowData.id + "IsDetail\"," + rowIndex + ")' />";
    } else {
        c = "<input type='checkbox' id='" + rowData.id + "IsDetail' value='N' onclick='onCheckIsDetail(\"" + rowData.id + "IsDetail\"," + rowIndex + ")' />";
    }
    return c;
}

/**
 * 【详细显示】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsDetail(id, rowIndex) {
    if (endEditing()) {
        $("#commTempletFieldDataGrid").datagrid('removeEditor', 'colIsDetail'); // 删除【详细显示】编辑器
        $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【详细显示】编辑器
            field: 'colIsDetail',
            editor: {
                type: 'text'
            }
        });
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[11];
            sfgzEditor.target.val("Y");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        } else {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[11];
            sfgzEditor.target.val("N");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        }
    } else {
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            document.getElementById(id).checked = false;
        } else {
            document.getElementById(id).checked = true;
        }
    }
}

/**
 * 【查询字段】转换方法
 * 
 * @param value
 * @param rowData
 * @param rowIndex
 * @returns {String}
 */
function fomaterIsSearch(value, rowData, rowIndex) {
    var c = "";
    if (rowData.colIsSearch == "Y") {
        c = "<input type='checkbox' id='" + rowData.id + "IsSearch' checked='checked' value='Y' onclick='onCheckIsSearch(\"" + rowData.id + "IsSearch\"," + rowIndex + ")' />";
    } else {
        c = "<input type='checkbox' id='" + rowData.id + "IsSearch' value='N' onclick='onCheckIsSearch(\"" + rowData.id + "IsSearch\"," + rowIndex + ")' />";
    }
    return c;
}

/**
 * 【查询字段】复选框值发生改变更新datagrid对应的值
 * 
 * @returns
 */
function onCheckIsSearch(id, rowIndex) {
    if (endEditing()) {
        $("#commTempletFieldDataGrid").datagrid('removeEditor', 'colIsSearch'); // 删除【查询字段】编辑器
        $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【查询字段】编辑器
            field: 'colIsSearch',
            editor: {
                type: 'text'
            }
        });
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[12];
            sfgzEditor.target.val("Y");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        } else {
            $('#commTempletFieldDataGrid').datagrid('selectRow', rowIndex)
                .datagrid('beginEdit', rowIndex);
            var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                rowIndex);
            var sfgzEditor = editors[12];
            sfgzEditor.target.val("N");
            $('#commTempletFieldDataGrid').datagrid('endEdit', rowIndex);
        }
    } else {
        var checkbox = document.getElementById(id);
        var isCheck = false;
        if (checkbox.checked) {
            isCheck = true;
        }
        if (isCheck) {
            document.getElementById(id).checked = false;
        } else {
            document.getElementById(id).checked = true;
        }
    }
}

/**
 * 单元格点击事件
 * 
 * @param index
 * @param field
 * @param value
 */
function onClickCell(index, field, value) {
    var rowDataNew = $('#commTempletFieldDataGrid').datagrid('getData').rows[index]; // 当前行
    var isNewNew = "";
    if (rowDataNew != undefined) {
        isNewNew = rowDataNew.isNew;
    }
    
    if(treeType == "root" && orgId != "ORG_ROOT"){
	   	 reloadDataGridByFieldId(rowDataNew.id); // 刷新代码表
	   	 return;
    }
   
    // 验证点击之前的行
    if (editIndex == index || endEditing()) {
        $('#commTempletFieldDataGrid').datagrid('endEdit', editIndex);
        if (isNewNew == "Y") { // 新增行
            if (field == 'colLabel') {
                changeName(editIndex, index, field);
            }
            $('#commTempletFieldDataGrid').datagrid('selectRow', index)
                .datagrid('editCell', {
                    index: index,
                    field: field
                });
        } else { // 加载行
            if (field == 'colName' || field == 'colLabel' || field == 'colType' || field == 'colLength' || field == 'colDecimal' || field == 'colName' || field == 'colType' || field == 'colLength' || field == 'colDecimal' || field == 'colOrder' || field == 'attribute11' || field == 'remark' || field == 'colDropdownType') {
                $('#commTempletFieldDataGrid').datagrid('selectRow', index)
                    .datagrid('editCell', {
                        index: index,
                        field: field
                    });
            }
        }
        reloadDataGridByFieldId(rowDataNew.id); // 刷新代码表
        editIndex = index;
    }
}

/**
 * 编辑数据
 */
function endEditing() {
    if (editIndex == undefined) {
        return true;
    }
    if ($('#commTempletFieldDataGrid').datagrid('validateRow', editIndex)) {
        $('#commTempletFieldDataGrid').datagrid('endEdit', editIndex);
        var rowData = $('#commTempletFieldDataGrid').datagrid('getData').rows[editIndex];
        if (rowData == undefined) {
            return true;
        }
        var colLabel = rowData.colLabel;
        if (colLabel == "" || colLabel == undefined) {
            $.messager.show({
                title: commonLang.tip.alert,//'提示'
                //commonLang.tip.content3   "第"  commonLang.tip.content4 "行【字段中文名】不能为空！"
                msg: commonLang.tip.content3 + (editIndex + 1) +commonLang.tip.content4 ,
                timeout: 3000,
                showType: 'slide'
            });
            $('#commTempletFieldDataGrid').datagrid('selectRow', editIndex)
                .datagrid('editCell', {
                    index: editIndex,
                    field: 'colLabel'
                });
            return false;
        }

        var colName = rowData.colName;
        var regn = /^(?![0-9])\w+$/; // 字段名正则（包含字母数字下划线，不能已数字开头，20位）
        if (colName == "" || colName == undefined || !regn.test(colName)) {
            $.messager.show({
                title:  commonLang.tip.alert,//'提示',
                //commonLang.tip.content3   "第"  commonLang.tip.content5 "行【字段名】输入非法，请查证！"
                msg: commonLang.tip.content3 + (editIndex + 1) +  commonLang.tip.content5,
                timeout: 3000,
                showType: 'slide'
            });
            $('#commTempletFieldDataGrid').datagrid('selectRow', editIndex)
                .datagrid('editCell', {
                    index: editIndex,
                    field: 'colName'
                });
            return false;
        }
        var colType = rowData.colType; // 字段类型
        var colTypeValue = "";
        if (colType == 'string') {
            colTypeValue = commonLang.dataType.string;//"字符串";
        } else if (colType == 'integer') {
            colTypeValue = commonLang.dataType.integer;//"整型";
        } else if (colType == 'date') {
            colTypeValue = commonLang.dataType.date;//"日期时间型";
        } else if (colType == 'float') {
            colTypeValue = commonLang.dataType.float;//"浮点型";
        } else if (colType == 'blob') {
            colTypeValue = "BLOB";
        } else if (colType == 'clob') {
            colTypeValue = "CLOB";
        }
        var colLength = rowData.colLength; // 长度
        var colDecimal = rowData.colDecimal; // 小数位数
        if (colType == "string" && colLength > 4000) { // 验证长度
            $.messager.show({
                title:  commonLang.tip.alert,//'提示',
                //commonLang.tip.content3   "第"  commonLang.tip.content6 "行，因为选择的【字段类型】为 commonLang.tip.content7 ”，因此【长度】的值不能超过

                msg: commonLang.tip.content3  + (editIndex + 1) + commonLang.tip.content6 + colTypeValue + commonLang.tip.content7+ "4000！",
                timeout: 3000,
                showType: 'slide'
            });
            $('#commTempletFieldDataGrid').datagrid('selectRow', editIndex)
                .datagrid('editCell', {
                    index: editIndex,
                    field: 'colLength'
                });
            return false;
        }
        if ((colType == "integer" || colType == "float") && colLength > 16) { // 验证长度
            $.messager.show({
                title: commonLang.tip.alert,//'提示',
                //commonLang.tip.content3   "第"  commonLang.tip.content6 "行，因为选择的【字段类型】为 commonLang.tip.content7 ”，因此【长度】的值不能超过

                msg: commonLang.tip.content3   + (editIndex + 1) +  commonLang.tip.content6 + colTypeValue +  commonLang.tip.content7+"16！",
                timeout: 3000,
                showType: 'slide'
            });
            $('#commTempletFieldDataGrid').datagrid('selectRow', editIndex)
                .datagrid('editCell', {
                    index: editIndex,
                    field: 'colLength'
                });
            return false;
        }
        if (colType == "float" && colDecimal > 6) { // 验证小数位数
            $.messager.show({
                title: commonLang.tip.alert,//'提示',
                //commonLang.tip.content3   "第"  commonLang.tip.content6 "行，因为选择的【字段类型】为 commonLang.tip.content7 ”，因此【长度】的值不能超过

                msg: commonLang.tip.content3+ (editIndex + 1) + commonLang.tip.content6 + colTypeValue + commonLang.tip.content7+"6！",
                timeout: 3000,
                showType: 'slide'
            });
            $('#commTempletFieldDataGrid').datagrid('selectRow', editIndex)
                .datagrid('editCell', {
                    index: editIndex,
                    field: 'colDecimal'
                });
            return false;
        }
        if (colType == "float" && (colDecimal == undefined || colDecimal == "")) { // 验证小数位数
            $.messager.show({
                title: commonLang.tip.alert,//'提示',
                //commonLang.tip.content3   "第"  commonLang.tip.content6 "行，因为选择的【字段类型】为 commonLang.tip.content9 ”，因此【小数位数】的值不能为空！"

                msg: commonLang.tip.content3 + (editIndex + 1) +commonLang.tip.content6 + colTypeValue +  commonLang.tip.content9 ,
                timeout: 3000,
                showType: 'slide'
            });
            $('#commTempletFieldDataGrid').datagrid('selectRow', editIndex)
                .datagrid('editCell', {
                    index: editIndex,
                    field: 'colDecimal'
                });
            return false;
        }
        if ((colType == "string" || colType == "date" || colType == "clob" || colType == "integer" || colType == "blob") && colDecimal != "" && colDecimal != undefined) { // 验证小数位数
            $.messager.show({
                title:  commonLang.tip.alert,//'提示',
                //commonLang.tip.content3   "第"  commonLang.tip.content6 "行，因为选择的【字段类型】为 commonLang.tip.content10 ”，，因此【小数位数】的值必须为空！"

                msg: commonLang.tip.content3 + (editIndex + 1) + commonLang.tip.content6 + colTypeValue +commonLang.tip.content10,
                timeout: 3000,
                showType: 'slide'
            });
            $('#commTempletFieldDataGrid').datagrid('selectRow', editIndex)
                .datagrid('editCell', {
                    index: editIndex,
                    field: 'colDecimal'
                });
            return false;
        }
        if ((colType == "date" || colType == "clob" || colType == "blob") && colLength != "" && colLength != undefined) { // 验证长度
            $.messager.show({
                title: commonLang.tip.alert,//'提示',
                //commonLang.tip.content3   "第"  commonLang.tip.content6 "行，因为选择的【字段类型】为 commonLang.tip.content19 ”，因此【长度】的值必须为空"

                msg: commonLang.tip.content3 + (editIndex + 1) +commonLang.tip.content6+ colTypeValue + commonLang.tip.content19,
                timeout: 3000,
                showType: 'slide'
            });
            $('#commTempletFieldDataGrid').datagrid('selectRow', editIndex)
                .datagrid('editCell', {
                    index: editIndex,
                    field: 'colLength'
                });
            return false;
        }
        return true;
    } else {
        $.messager.show({
            title: commonLang.tip.alert,//'提示',
            //commonLang.tip.checkData "请检查数据是否正确！"
            msg: commonLang.tip.checkData,
            timeout: 3000,
            showType: 'slide'
        });
        return false;
    }
}

/**
 * 根据中文名称改变英文名称
 * 
 * @param index
 */
function changeName(editIndex, index, field) {
    $('#commTempletFieldDataGrid').datagrid('selectRow', editIndex).datagrid(
        'beginEdit', editIndex);
    // 定义事件
    var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
        editIndex);
    var sfgzEditor = editors[0];
    sfgzEditor.target.bind('change', function() {
        var value = sfgzEditor.target.val();
        // 请求后台，转换字段名称
        $.ajax({
            url: 'platform/aos/commtempletmanagerController/onChangeCol',
            data: {
                name: value
            },
            type: 'post',
            dataType: 'json',
            success: function(result) {
                var editors = $('#commTempletFieldDataGrid').datagrid(
                    'getEditors', editIndex);
                var sfgzEditor = editors[1];
                sfgzEditor.target.val(result.textpinyin);
            }
        });
    });
    $('#commTempletFieldDataGrid').datagrid('endEdit', editIndex);

    $('#commTempletFieldDataGrid').datagrid('selectRow', index).datagrid(
        'editCell', {
            index: index,
            field: field
        });
    editIndex = index;
}
/**
 * 追加行
 */
function append() {
    $
        .ajax({
            url: 'platform/aos/commtempletmanagerController/getColMaxOrder.json',
            data: {
                sysId: sysId,
                templetId: templetId
            },
            type: 'post',
            dataType: 'json',
            success: function(result) {
                if (endEditing()) {
                    $("#commTempletFieldDataGrid").datagrid('removeEditor', 'colIsSys'); // 删除【是否系统字段】编辑器
                    $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否系统字段】编辑器
                        field: 'colIsSys',
                        editor: {
                            type: 'checkbox',
                            options: {
                                on: 'Y',
                                off: 'N'
                            }
                        }
                    });
                    $("#commTempletFieldDataGrid").datagrid('removeEditor',
                        'colIsDisplay'); // 删除【是否可见】编辑器
                    $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否可见】编辑器
                        field: 'colIsDisplay',
                        editor: {
                            type: 'checkbox',
                            options: {
                                on: 'Y',
                                off: 'N'
                            }
                        }
                    });
                    $("#commTempletFieldDataGrid").datagrid('removeEditor',
                        'colIsMust'); // 删除【是否必著】编辑器
                    $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否必著】编辑器
                        field: 'colIsMust',
                        editor: {
                            type: 'checkbox',
                            options: {
                                on: 'Y',
                                off: 'N'
                            }
                        }
                    });
                    $("#commTempletFieldDataGrid").datagrid('removeEditor',
                        'colIsVisible'); // 删除【是否显示】编辑器
                    $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否显示】编辑器
                        field: 'colIsVisible',
                        editor: {
                            type: 'checkbox',
                            options: {
                                on: 'Y',
                                off: 'N'
                            }
                        }
                    });
                    $("#commTempletFieldDataGrid").datagrid('removeEditor',
                        'colIsEdit'); // 删除【是否编辑】编辑器
                    $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否编辑】编辑器
                        field: 'colIsEdit',
                        editor: {
                            type: 'checkbox',
                            options: {
                                on: 'Y',
                                off: 'N'
                            }
                        }
                    });
                    $("#commTempletFieldDataGrid").datagrid('removeEditor',
                        'colIsTabVisible'); // 删除【是否列表显示】编辑器
                    $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否列表显示】编辑器
                        field: 'colIsTabVisible',
                        editor: {
                            type: 'checkbox',
                            options: {
                                on: 'Y',
                                off: 'N'
                            }
                        }
                    });
                    $("#commTempletFieldDataGrid").datagrid('removeEditor',
                        'colIsDetail'); // 删除【是否详细显示】编辑器
                    $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【是否详细显示】编辑器
                        field: 'colIsDetail',
                        editor: {
                            type: 'checkbox',
                            options: {
                                on: 'Y',
                                off: 'N'
                            }
                        }
                    });
                    $("#commTempletFieldDataGrid").datagrid('removeEditor',
                        'colIsSearch'); // 删除【查询字段】编辑器
                    $("#commTempletFieldDataGrid").datagrid('addEditor', { // 添加【查询字段】编辑器
                        field: 'colIsSearch',
                        editor: {
                            type: 'checkbox',
                            options: {
                                on: 'Y',
                                off: 'N'
                            }
                        }
                    });
                    var rid = new Date().getTime();
                    var num;
                    var rows = $("#commTempletFieldDataGrid").datagrid(
                        "getRows");
                    if (rows == "" || rows == null) {
                        num = 1;
                    } else {
                        num = result.num + 1;
                    }
                    $('#commTempletFieldDataGrid').datagrid('appendRow', {
                        id: rid,
                        colOrder: num,
                        attribute11: num,
                        colType: 'string',
                        colLength: '30',
                        colIsDisplay: 'Y',
                        colIsMust: 'N',
                        colIsSys: 'N',
                        colIsVisible: 'Y',
                        colIsEdit: 'Y',
                        colIsTabVisible: 'N',
                        colIsDetail: 'Y',
                        colIsSearch: 'N',
                        colDropdownType: '0',
                        isNew: 'Y'
                    });
                    $('#commTempletFieldDataGrid').datagrid('endEdit',
                        editIndex);
                    editIndex = $('#commTempletFieldDataGrid').datagrid(
                        'getRows').length - 1;
                    onClickCell(editIndex, "colLabel", "");
                    // 将小数位数设置为只读
                    var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
                        editIndex);
                    var sfgzEditor2 = editors[4];
                    var decimalInput = sfgzEditor2.target; // 得到文本框对象
                    decimalInput.numberbox('setValue', "");
                    decimalInput.prop('readonly', true); // 设值只读
                }
            }
        });
}

/**
 * 提交改变的记录
 */
function accept() {
	
    if (endEditing()) {
        // 新增字段
        var insertRows = $("#commTempletFieldDataGrid").datagrid('getChanges',
            'inserted');
        var insertData = JSON.stringify(insertRows);
        // 更新字段
        var updateRows = $("#commTempletFieldDataGrid").datagrid('getChanges',
            'updated');
        var updateData = JSON.stringify(updateRows);
        if (insertRows == "" && updateRows == "") {
            //commonLang.tip.alert '提示',commonLang.tip.alert2参数替换'没有可保存的记录！'
        	$.messager.alert(commonLang.tip.alert ,commonLang.tip.alert2,'warning');
            return false;
        }
        //进行字段名重复验证
        var rows = $("#commTempletFieldDataGrid").datagrid('getRows');
        var rowsNum = JSON.stringify(rows.length);
        for (var i = 0; i < rowsNum; i++) {
            var colName = rows[i].colName;
            for (var j = 0; j < rowsNum; j++) {
                var colNameTemp = rows[j].colName;
                if (i != j) {
                    if (colName == colNameTemp) {
                        //commonLang.tip.alert'提示',commonLang.tip.filed参数替换'字段'，commonLang.tip.alert3 '已存在，请检查！'
                    	$.messager.alert(commonLang.tip.alert,commonLang.tip.filed + colName + commonLang.tip.alert3,'warning');
                        return false;
                    }
                }
            }
        }
        
        /*if(!duplicateCheck(rows)){
        	return false;
        }*/
        
        $.ajax({
                url: 'platform/aos/commtempletmanagerController/insertOrUpdateCommTempletFieldAll.json',
                data: {
                    insertData: insertData,
                    updateData: updateData,
                    sysId: sysId,
                    templetId: templetId
                },
                type: 'post',
                dataType: 'json',
                success: function(result) {
                    if (result.flag == "failure") {
                        $.messager.show({
                            title: commonLang.tip.alert,//'提示',
                            msg: result.mes
                        });
                    } else {
                        isOk = true;
                        $.messager.show({
                            title: commonLang.tip.alert,//'提示',
                            //commonLang.optip.saveSuccess "保存成功！"
                            msg: commonLang.optip.saveSuccess
                        });
                    }
                    reloadTempletInfo();
                }
            });
    }
}

/**
 * 导出结构模版
 */
function exportExcelMoudle() {
    var url = baseurl + 'platform/aos/commtempletmanagerController/exportExcelMoudle';
    window.open(url);
}

/**
 * 导出结构数据
 */
function exportData() {
    var url = baseurl + 'platform/aos/commtempletmanagerController/exportData?templetId=' + templetId;
    var rows = $("#commTempletFieldDataGrid").datagrid("getRows");
    if (rows == "" || rows == null) {
        //commonLang.tip.alert '提示',commonLang.tip.content11参数替换'请先添加数据再导出excel文件！'
    	$.messager.alert(commonLang.tip.alert,commonLang.tip.content11,'warning');
        return;
    } else {
        window.open(url);
    }
}

/**
 * 导入对话框
 */
function openImportDialog() {
    var dialogId = "FormImportDialog";
    var toActionUrl = 'avicit/cbb/techclass/tabledefine/commtempletmanager/table_templet_field_import.jsp?sysId=' + sysId + '&templetId=' + templetId+'&dialogId=' +dialogId;
    //commonLang.tip.content12 "导入结构数据"
    var usdImport = new CommonDialog(dialogId, "530", "220", toActionUrl, commonLang.tip.content12,
        false, true, false);
    usdImport.show();
}

/**
 * 查询按钮
 */
function searchField() {
    var dialogId = "searchFormShowDialog";
    var toActionUrl = "platform/aos/commtempletmanagerController/toTempletSearchForm?dialogId=" + dialogId;
    //commonLang.dialog.search "查询"
    var usdAdd = new CommonDialog(dialogId, "800", "200", toActionUrl, commonLang.dialog.search,
        false, true, false);
    usdAdd.show();
}

/**
 * 查询子页面回调函数
 */
function searchFieldDataGrid(colLabel, colName) {
    if (colName != "" && colName != null) {
        colName = encodeURI(colName);
    }
    if (colLabel != "" && colLabel != null) {
        colLabel = encodeURI(colLabel);
    }
    var node;
	if(treeType == "root"){
		node = $('#commTempletRootTree').tree('getSelected');
	}else{
		node = $('#commTempletInfoTree').tree('getSelected');
	}
    var tid = node.id;
    var url = "platform/aos/commtempletmanagerController/getCommTempletFieldData.json?colName=" + colName + "&colLabel=" + colLabel + "&tid=" + tid;
    $('#commTempletFieldDataGrid').datagrid("options").url = url;
    $("#commTempletFieldDataGrid").datagrid('load', {
        param: null
    });
    reloadDataGridByFieldId("@@@");
}

/**
 * 通用代码表点击行事件
 */
function onClickRowCode(rowIndex, field, value) {
    // 判断是否为系统数据
    var ctype = $('#commTempletCodeDataGrid').datagrid('getSelected').CTYPE;
    if (ctype == "W") {
        // 设置编辑行
        if (editCodeIndex != rowIndex) {
            if (endCodeEditing()) {
                $('#commTempletCodeDataGrid').datagrid('selectRow', rowIndex)
                    .datagrid('beginEdit', rowIndex);
                editCodeIndex = rowIndex;
            } else {
                $('#commTempletCodeDataGrid').datagrid('selectRow',
                    editCodeIndex);
            }
        }
    }
}

/**
 * 增加手写通用代码
 */
function appendCode() {
    if ($('#commTempletFieldDataGrid').datagrid("getSelected") != null) {
        var isNew = $('#commTempletFieldDataGrid').datagrid("getSelected").isNew; // 是否为新建行标识
        if (isNew == "Y") {
            // commonLang.tip.alert'提示',commonLang.tip.content14 '请先保存字段，再添加通用代码！'
        	$.messager.alert( commonLang.tip.alert,commonLang.tip.content14,'warning');
        } else {
            var num = $('#commTempletCodeDataGrid').datagrid('getRows').length + 1;
            if (endCodeEditing()) {
                $('#commTempletCodeDataGrid').datagrid('appendRow', {
                    ID: '',
                    CODE_IS_VALID: 'Y',
                    CODE_ORDER: num,
                    CTYPE: 'W'
                });
                editCodeIndex = $('#commTempletCodeDataGrid').datagrid(
                    'getRows').length - 1;
                $('#commTempletCodeDataGrid').datagrid('selectRow',
                    editCodeIndex).datagrid('beginEdit', editCodeIndex);
                changeCodeName(editCodeIndex);
            }
        }
    } else {
        //commonLang.tip.alert'提示',commonLang.tip.content15 '请选中字段后，再进行操作！'
    	$.messager.alert(commonLang.tip.alert,commonLang.tip.content15,'warning');
    }
}

/**
 * 通用代码提交验证
 * 
 * @returns {Boolean}
 */
function endCodeEditing(content3) {
    if (editCodeIndex == undefined) {
        return true;
    }
    if ($('#commTempletCodeDataGrid').datagrid('validateRow', editCodeIndex)) {
        $('#commTempletCodeDataGrid').datagrid('endEdit', editCodeIndex);
        editCodeIndex = undefined;
        return true;
    } else {
        $.messager.show({
            title:  commonLang.tip.alert,//'提示',
            //commonLang.tip.checkData "请检查数据是否正确!"
            msg: commonLang.tip.checkData,
            timeout: 2000,
            showType: 'slide'
        });
        return false;
    }
}

/**
 * 通用代码保存方法
 */
function saveCode() {
    if (endCodeEditing()) {
        // 新增字段
        var insertRowsCode = $("#commTempletCodeDataGrid").datagrid(
            'getChanges', 'inserted');
        var inserCodeData = JSON.stringify(insertRowsCode);
        // 更新字段
        var updateRowsCode = $("#commTempletCodeDataGrid").datagrid(
            'getChanges', 'updated');
        var updateCodeData = JSON.stringify(updateRowsCode);
        if (insertRowsCode == "" && updateRowsCode == "") {
            // commonLang.tip.alert'提示',commonLang.tip.alert2 '没有可保存的记录！'
        	$.messager.alert( commonLang.tip.alert,commonLang.tip.alert2,'warning');
            return false;
        }
        //循环获取每一行数据进行重复验证
        var rows = $("#commTempletCodeDataGrid").datagrid('getRows');
        for (var i = 0; i < rows.length; i++) {
            var name = rows[i].LOOKUP_NAME;
            var code = rows[i].LOOKUP_CODE;
            for (var j = i + 1; j < rows.length; j++) {
                var name2 = rows[j].LOOKUP_NAME;
                var code2 = rows[j].LOOKUP_CODE;
                if (name == name2) {
                    $.messager.show({
                        title:  commonLang.tip.alert,//'提示',
                        //commonLang.dialog.displayValue 替换"显示值"，commonLang.dialog.repeat替换"重复！"
                        msg: commonLang.dialog.displayValue +"["+ name2 +"]" +commonLang.dialog.repeat,
                        timeout: 2000,
                        showType: 'slide'
                    });
                    return false;
                }
                if (code == code2) {
                    $.messager.show({
                        title: commonLang.tip.alert,//'提示',
                        //commonLang.dialog.trueValue替换"真实值"，commonLang.dialog.repeat替换"重复！"
                        msg: commonLang.dialog.trueValue+"[" + code2 + "]"+commonLang.dialog.repeat,
                        timeout: 2000,
                        showType: 'slide'
                    });
                    return false;
                }
            }
        }
        var node;
    	if(treeType == "root"){
    		node = $('#commTempletRootTree').tree('getSelected');
    	}else{
    		node = $('#commTempletInfoTree').tree('getSelected');
    	}
    	
        var tid = node.id;
        var row = $("#commTempletFieldDataGrid").datagrid('getSelections');
        if (row.length == 1) {
            var templetId = tid;
            var isNew = row[0].isNew;
            var fieldId = row[0].id;
            if (isNew == "Y") {
                //commonLang.tip.alert'提示',commonLang.tip.content14 '请选保存字段，再保存通用代码！'
            	$.messager.alert(commonLang.tip.alert,commonLang.tip.content14,'warning');
            } else {
                $
                    .ajax({
                        url: 'platform/aos/commtempletmanagerController/saveTempletFieldCodeData.json',
                        data: {
                            inserCodeData: inserCodeData,
                            updateCodeData: updateCodeData,
                            sysId: sysId,
                            templetId: templetId,
                            fieldId: fieldId
                        },
                        type: 'post',
                        dataType: 'json',
                        success: function(result) {
                            if (result.flag == "success") {
                                $.messager.show({
                                    title: commonLang.tip.alert,//'提示',
                                    // commonLang.optip.saveSuccess "保存成功！"
                                    msg: commonLang.optip.saveSuccess
                                });
                                var cid = $('#commTempletFieldDataGrid')
                                    .datagrid("getSelected").id;
                                reloadDataGridByFieldId(cid); // 刷新代码表
                            } else {
                                $.messager.show({
                                    title: commonLang.tip.alert,//'提示',
                                    msg: result.mes
                                });
                            }
                        }
                    });
            }
        }
    }
}

/**
 * 根据主表ID刷新子表表格数据
 */
function reloadDataGridByFieldId(fieldId) {
	var node;
	if(treeType == "root"){
		node = $('#commTempletRootTree').tree('getSelected');
	}else{
		node = $('#commTempletInfoTree').tree('getSelected');
	}
    var tid = node.id;
    var url = "platform/aos/commtempletmanagerController/getCommTempletCodeData.json?fid=" + fieldId + "&tid=" + tid;
    $('#commTempletCodeDataGrid').datagrid("options").url = url;
    $("#commTempletCodeDataGrid").datagrid('load', {
        param: null
    });
}

/**
 * 删除代码维护
 */
function deleteWcode(confirm,confirmContent,success,failure,remind,content) {
	var node;
	if(treeType == "root"){
		node = $('#commTempletRootTree').tree('getSelected');
	}else{
		node = $('#commTempletInfoTree').tree('getSelected');
	}
    var tid = node.id;
    var rows = $("#commTempletCodeDataGrid").datagrid('getChecked');
    if (rows.length > 0) {
        $.messager
            .confirm(
                //commonLang.tip.confirm替换'请确认'，commonLang.tip.confirmContent替换'您确定要删除当前所选的记录？'
            		commonLang.tip.confirm,
            		commonLang.tip.confirmContent,
                function(b) {
                    if (b) {
                        var frow = $("#commTempletFieldDataGrid")
                            .datagrid('getSelections');
                        var fid = frow[0].id;
                        var deleteData = JSON.stringify(rows);
                        $.ajax({
                                url: 'platform/aos/commtempletmanagerController/deleteCommTempletCode',
                                data: {
                                    deleteData: deleteData,
                                    fieldId: fid,
                                    templetId: tid,
                                    sysId: sysId
                                },
                                type: 'post',
                                dataType: 'json',
                                success: function(result) {
                                    if (result.flag == "success") {
                                        reloadDataGridByFieldId(fid);
                                        $.messager.show({
                                            title:  commonLang.tip.alert,//'提示',
                                            //commonLang.optip.success替换"删除成功！"
                                            msg: commonLang.optip.success,
                                            timeout: 2000,
                                            showType: 'slide'
                                        });
                                    } else {
                                        if (result.flag == "failure") {
                                            $.messager.show({
                                                title: commonLang.tip.alert,//'提示',
                                                //commonLang.optip.failure 替换"删除失败！"
                                                msg: commonLang.optip.failure,
                                                timeout: 2000,
                                                showType: 'slide'
                                            });
                                        } else {
                                            $.messager.alert(commonLang.tip.alert,//'提示',
                                                result.flag,
                                                'warning');
                                        }
                                    }
                                }
                            });
                    }
                });
    } else {
        //commonLang.tip.alert替换'提示',commonLang.tip.content2 替换'请选择要删除的记录！'
    	$.messager.alert(commonLang.tip.alert, commonLang.tip.content2, 'warning');
    }
}

/**
 * 选择系统通用代码
 */
function selectCode() {
    if ($('#commTempletFieldDataGrid').datagrid("getSelected") != null) {
    	var node;
    	if(treeType == "root"){
    		node = $('#commTempletRootTree').tree('getSelected');
    	}else{
    		node = $('#commTempletInfoTree').tree('getSelected');
    	}
        var tid = node.id;
        var row = $('#commTempletFieldDataGrid').datagrid('getSelections');
        if (row.length == 1) {
            var fid = row[0].id;
            var dialogId = "wcodeAddDialog";
            var toActionUrl = "platform/aos/commtempletmanagerController/toCommTempletScodeAdd?dialogId=" + dialogId + "&tId=" + tid + "&fid=" + fid;
            //commonLang.dialog.select 替换"选择"
            var usdAdd = new CommonDialog(dialogId, "800", "435", toActionUrl,
            		commonLang.dialog.select, false, true, false, "", true);
            usdAdd.show();
        }
    } else {
        // commonLang.tip.alert 替换'提示',commonLang.tip.operateAfter 替换'请选中字段后，再进行操作！'
    	$.messager.alert( commonLang.tip.alert,commonLang.tip.operateAfter,'warning');
    }
}

/**
 * 转换类型
 * 
 * @param value
 * @param row
 * @param index
 * @returns
 */
function fomaterCType(value, row, index) {
    if (value == 'W') {
        return commonLang.dialog.buildByOnself;//"自建";
    } else {
        return commonLang.dialog.system;//"系统";
    }
    return value;
}
/**
 * 转换有效无效
 * 
 * @param value
 * @param row
 * @param index
 * @returns {String}
 */
function fomaterVa(value, row, index) {
    if (value == 'Y') {
        return commonLang.dialog.effective;//"有效";
    } else {
        return commonLang.dialog.invalid;//"无效";
    }
    return value;
}

/**
 * 【下拉类型】转换
 * 
 * @param value
 * @param row
 * @param index
 * @returns
 */
function fomaterType(value, row, index) {
    var type = "";
    if (value == '0') {
        type = commonLang.fomaterType.value0;//"无";
    } else if (value == '1') {
        type = commonLang.fomaterType.value1;//"参选";
    } else if (value == '2') {
        type = commonLang.fomaterType.value2;//"只选";
    } else if (value == '3') {
        type = commonLang.fomaterType.value3;//"选择";
    }
    return type;
}

/**
 * 类型下拉选择事件
 * 
 * @param record
 */
function selectTypeOnHidePanel() {
    var row = $("#commTempletFieldDataGrid").datagrid('getSelected');
    var index = $("#commTempletFieldDataGrid").datagrid('getRowIndex', row);
    $('#commTempletFieldDataGrid').datagrid('endEdit', index);
    $('#commTempletFieldDataGrid').datagrid('selectRow', index).datagrid(
        'beginEdit', index);
    var colDropdownTypeNew = selectTypeCode;
    var colType = row.colType;
    var colDropDownTypeNewName = selectTypeName;
    // integer，float，date类型数据只能配置为【空】
    if ('integer' == colType || 'float' == colType || 'date' == colType) {
        var colTypeName = '';
        if ('integer' == colType) {
            colTypeName =  commonLang.dataType.integer;//'整型';
        } else if ('float' == colType) {
            colTypeName = commonLang.dataType.float;//'浮点型';
        } else if ('date' == colType) {
            colTypeName = commonLang.dataType.date;//'日期时间型';
        }
        if ('0' != colDropdownTypeNew) { // 类型不为空，提示
            $.messager.show({
                title:  commonLang.tip.alert,//'提示',
                // commonLang.tip.content16 '字段不能配置为'
                msg: colTypeName +commonLang.tip.content16  + '【' + colDropDownTypeNewName + '】'
            });
            // 置为【无】
            var ed = $('#commTempletFieldDataGrid').datagrid('getEditor', {
                index: index,
                field: 'colDropdownType'
            });
            $(ed.target).combobox('select', '0');
            return false;
        }
    }
    if (JSON.stringify(colDropdownTypeNew) == '"3"') {
        var dialogId = "formShowDialog";
        var toActionUrl = "platform/cbb/billDefineManagerController/toRuleShow?dialogId=" + dialogId + "&sysId=" + sysId;
        var usdAdd = new CommonDialog(dialogId, "800", "400", toActionUrl,
        		//commonLang.dialog.typeSelection "类型选择"
        		commonLang.dialog.typeSelection , false, true, false);
        usdAdd.show();
    } else {
        var ed1 = $('#commTempletFieldDataGrid').datagrid('getEditor', {
            index: index,
            field: 'colRuleTitle'
        });
        $(ed1.target).val("");
        var ed2 = $('#commTempletFieldDataGrid').datagrid('getEditor', {
            index: index,
            field: 'colRuleName'
        });
        $(ed2.target).val("");
        var ed3 = $('#commTempletFieldDataGrid').datagrid('getEditor', {
            index: index,
            field: 'customPath'
        });
        $(ed3.target).val("");
    }
    $('#commTempletFieldDataGrid').datagrid('endEdit', index);
}

/**
 * 类型下拉选择事件
 * 
 * @param record
 */
function onSelectCom(record) {
    selectTypeCode = record.lookupCode;
    selectTypeName = record.lookupName;
}

/**
 * 字段类型选择事件
 * 
 * @param record
 */
function typeSelect(record) {
    type = record.lookupCode;
}
/**
 * 字段类型选择弹出面板事件
 */
function typeOnHidePanel() {
    if (type != undefined) {
        var length = ""; // 长度
        var lengthEdit = true; // 长度是否可编辑，默认只读
        var decimal = ""; // 小数位数
        var decimalEdit = true; // 小数位数是否可编辑，默认只读
        if (type == "string") {
            length = 30;
            lengthEdit = false;
        } else if (type == "integer") {
            length = 16;
            lengthEdit = false;
        } else if (type == "float") {
            length = 16;
            decimal = 2;
            lengthEdit = false;
            decimalEdit = false;
        }
        // 关闭行编辑
        $('#commTempletFieldDataGrid').datagrid('endEdit', editIndex);
        // 打开行编辑
        $('#commTempletFieldDataGrid').datagrid('selectRow', editIndex)
            .datagrid('beginEdit', editIndex);
        var editors = $('#commTempletFieldDataGrid').datagrid('getEditors',
            editIndex);
        var sfgzEditor1 = editors[3];
        var lengthInput = sfgzEditor1.target; // 得到文本框对象
        lengthInput.numberbox('setValue', length);
        lengthInput.prop('readonly', lengthEdit); // 可编辑
        var sfgzEditor2 = editors[4];
        var decimalInput = sfgzEditor2.target; // 得到文本框对象
        decimalInput.numberbox('setValue', decimal);
        decimalInput.prop('readonly', decimalEdit); // 可编辑
        // 关闭行编辑
        $('#commTempletFieldDataGrid').datagrid('endEdit', editIndex);
        // 打开【字段类型】单元格编辑
        onClickCell(editIndex, "colType", "");
        type = undefined;
    }
}

/**
 * 显示值给真实值
 * 
 * @param index
 */
function changeCodeName(editCodeIndex) {
    // 定义事件
    var editors = $('#commTempletCodeDataGrid').datagrid('getEditors',
        editCodeIndex);
    var sfgzEditor = editors[1];
    var sfgzEditor1 = editors[2];
    sfgzEditor.target.bind('change', function() {
        var value = sfgzEditor.target.val();
        sfgzEditor1.target.val(value);
    });
}

/**
 * 类型选择完成后回填值
 * 
 * @param selCode
 * @param selName
 * @param selUrl
 */
function endSelectCom(selCode, selName, selUrl) {
    var row = $("#commTempletFieldDataGrid").datagrid('getSelected');
    var index = $("#commTempletFieldDataGrid").datagrid('getRowIndex', row);
    $('#commTempletFieldDataGrid').datagrid('selectRow', index).datagrid(
        'beginEdit', index);
    var ed1 = $('#commTempletFieldDataGrid').datagrid('getEditor', {
        index: index,
        field: 'colRuleTitle'
    });
    $(ed1.target).val(selName);
    var ed2 = $('#commTempletFieldDataGrid').datagrid('getEditor', {
        index: index,
        field: 'colRuleName'
    });
    $(ed2.target).val(selCode);
    var ed3 = $('#commTempletFieldDataGrid').datagrid('getEditor', {
        index: index,
        field: 'customPath'
    });
    $(ed3.target).val(selUrl);
    var $input = ed1.target; // 得到文本框对象
    $input.prop('readonly', true); // 设值只读
    $('#commTempletFieldDataGrid').datagrid('endEdit', index);
}
/**
 * 当没有数据时，手动添加滚动条
 * 
 * @param data
 */
function onLoadSuccessData(data) {
    if (data.rows.length == 0) {
        $(this).prev(".datagrid-view2").children(".datagrid-body").html(
            "<div style='width:" + $(this).prev(".datagrid-view2").find(
                ".datagrid-header-row").width() + "px;border:solid 0px;height:1px;'></div>");
    }
}
/**
 * 关闭语言选择对话框
 */
function closeL(id){
	$("#chooseL").dialog('close');
};
//打开语言选择
function openLanguageForm(datagrid,type){
	var rows = $("#"+datagrid).datagrid('getChecked');
	if(rows.length !== 1){
		//commonLang.tip.alert替换'提示',commonLang.tip.alert5替换"请选择一条数据"
		$.messager.alert( commonLang.tip.alert,commonLang.tip.alert18,'warning');
		return false;
	}
	var id="";
	if(type=="1"){
		id=rows[0].id;
	}else{
		id=rows[0].ID;
	}
	//commonLang.dialog.i18nLanguageSet "多语言设置"
	this.cData = new CommonDialog("chooseL","700","400",'platform/aos/commtempletmanagerController/chooseLanguage/'+id+"/"+type,commonLang.dialog.i18nLanguageSet,false,true,false);

	this.cData.show();
};
//通用代码多语言选择
function openLanguageLookUpForm(datagrid,type){
	var rows = $("#"+datagrid).datagrid('getChecked');

	if(rows.length !== 1){
		// commonLang.tip.alert5 "请选择一条数据！"
		alert(commonLang.tip.alert5);
		return false;
	}
	if(type=="1"){
		id=rows[0].id;
	}else{
		id=rows[0].ID;
	}
	//commonLang.dialog.i18nLanguageSet "多语言设置"
	this.cData = new CommonDialog("chooseL","700","400",'platform/aos/commtempletmanagerController/chooseLanguage/'+id+"/"+type,commonLang.dialog.i18nLanguageSet,false,true,false);
	this.cData.show();
};


//字段校验，子数据与主数据校验是否有重复信息
function duplicateCheck(rows){
	var ok = false;
	var node;
	if(treeType == "root"){
		node = $('#commTempletRootTree').tree('getSelected');
	}else{
		node = $('#commTempletInfoTree').tree('getSelected');
	}
	//根据当前节点获得节点对应系统节点信息
	var sysNode = getSysNode(node);
	//获取系统节点同模板对应的字段信息
	$.ajax({
      url: 'platform/aos/commtempletmanagerController/duplicateCheck',
      data: {
      	 nodeRows : JSON.stringify(rows),
           sysNodeId : sysNode.id,
           tempType : sysNode.attributes.TEMP_TYPE,
           tempModelType : node.attributes.TEMP_MODEL_TYPE,
           sysId : sysId,
           treeType : treeType
      },
      async: false,
      type: 'post',
      dataType: 'json',
      success: function(result) {
          if (result.flag == "success") {
          	if(result.msg == "ok"){
          		ok = true;
          	}else{
          		$.messager.show({
                      title: commonLang.tip.alert,//'提示',
                      msg: result.msg,
                      timeout: 2000,
                      showType: 'slide'
                  });
          	}
          } else {
          	$.messager.show({
                  title: commonLang.tip.alert,//'提示',
                  msg: "后台处理异常，请联系管理员",
                  timeout: 2000,
                  showType: 'slide'
              });
          }
      }
  });
	return ok;
}

//得到当前节点对应的系统节点信息
function getSysNode(node){
	if(node.attributes.TEMP_TYPE == "S" || node.attributes.TEMP_TYPE == "R"){
		return node;
	}else{
		var parentNode;
		var target = node.target;
		if(treeType == "root"){
			parentNode = $('#commTempletRootTree').tree('getParent',target);
		}else{
			parentNode = $('#commTempletInfoTree').tree('getParent',target);
		}
		return getSysNode(parentNode);
	}
}

